package ani.trainning.cacobatph.das;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import ani.trainning.cacobatph.R;

public class SimpleAdapterLignesDas extends BaseAdapter{

	private final List<_LigneDas> lignesDas;
	private final LayoutInflater layoutInflater;
	private final Act6_Das context;;
	public SimpleAdapterLignesDas (Act6_Das context,List<_LigneDas>lignesDas){
		this.context=context;
		this.lignesDas=lignesDas;
		this.layoutInflater=LayoutInflater.from(context);}


	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public Object getItem(int arg0) {
		return lignesDas.get(arg0);
	}

	@Override
	public int getCount() {
		return lignesDas.size();
	}

	public static  class Holder {
		TextView textNom;
		TextView textPrenom;
		TextView textSalaire;
		ImageView imgEdit;
		ImageView imgDelete;




	}

	@Override
	public View getView(final int position,  View convertView, ViewGroup arg2) {
		final Holder holder;
		if(convertView==null){
			convertView=layoutInflater.inflate(R.layout.item_ligne_das, null);
			holder=new Holder();
			holder.textNom=(TextView) convertView.findViewById(R.id.nomItem);
			holder.textPrenom=(TextView) convertView.findViewById(R.id.prenomItem);
			holder.textSalaire=(TextView) convertView.findViewById(R.id.salaireItem);
			holder.imgEdit=(ImageView)convertView.findViewById(R.id.editItem);
			holder.imgDelete=(ImageView)convertView.findViewById(R.id.deleteItem);
			convertView.setTag(holder);
		}
		else{
			holder=(Holder) convertView.getTag();

		}

		holder.textNom.setText(lignesDas.get(position).getNom());
		holder.textPrenom.setText(lignesDas.get(position).getPrenom());
		holder.textSalaire.setText(lignesDas.get(position).getSalaireAnnuel()+"");
		holder.imgEdit.setImageResource(R.drawable.edit);
		holder.imgDelete.setImageResource(R.drawable.delete);
		holder.imgEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				final View viewForm=layoutInflater.inflate(R.layout.lay_saisie_salarie, null);
				_LigneDas ligne=lignesDas.get(position);

				new PopupSaisieFormulaire(context,viewForm,ligne).addListenerEditLigneDas(new ListenerEditLigneDas() {

					@Override
					public void onEditLigneDas(_LigneDas ligne) {
						holder.textNom.setText(ligne.getNom());
						holder.textPrenom.setText(ligne.getPrenom());
						holder.textSalaire.setText(ligne.getSalaireAnnuel()+"");
						lignesDas.remove(position);
						lignesDas.add(position, ligne);
						notifyDataSetChanged();


					}
				});

			}
		});
		holder.imgDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				lignesDas.remove(position);
				notifyDataSetChanged();


			}
		});
		return convertView;
	}





}
